package org.compression.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.annotation.Resource;

import org.compression.services.imp.AduioAttrServiceImp;
import org.compression.services.imp.AudioServiceImp;
import org.model.business.AduioAttr;
import org.model.business.BookMagament;
import org.model.comm.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping(value = "/aduioAttr")
public class AduioAttrController extends BaseController<AduioAttrServiceImp,AduioAttr>{


	@Value("${file.path}")
	public String filepath;

	@Value("${file.pathImage}")
	public String pathImage;
	
	@Autowired
	AduioAttrServiceImp mAduioAttrServiceImp;
	

	@RequestMapping(value ="/upfile/{createdate}", method = RequestMethod.POST,produces="application/json; utf-8")
	public Object insertBook(@RequestParam("file") MultipartFile file,@PathVariable String  createdate) {
		System.out.println(createdate);
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(pathImage+createdate+file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
				AduioAttr mAduioAttr=mAduioAttrServiceImp.selectCreateTime(createdate);
				mAduioAttr.setPath("pathImage/"+createdate+file.getOriginalFilename());
				mAduioAttr.setAttr(filepath+createdate+file.getOriginalFilename());
				mAduioAttrServiceImp.updateByPrimaryKeySelective(mAduioAttr);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
			return "上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}
		
		
	}


}
